# 抓取金十数据新闻消息示例
import os, time, random, requests, json
import pandas as pd
import json, csv
# 增加UserAgent，防止被反爬虫拦截
# 安装：pip install fake_useragent
from fake_useragent import UserAgent


def get_header():
    # 构造useragent，为避免网络访问时延及报错，fake_useragent.json存放到本地
    location = os.path.dirname(__file__) + '/fake_useragent.json'
    ua = UserAgent()
    # ua = UserAgent()
    return ua.random


def get_jin10():
    del_list = ['<b>', '</b>', '<br/>', '<span class="section-news">', '</span>']
    url = "https://www.jin10.com/flash_newest.js?t=1667728303028"
    headers = {'User-Agent': get_header()}
    response = requests.get(url, headers=headers, timeout=3)
    response.encoding = 'utf-8'
    Json = json.loads(response.text[13:-1])
    content_list = []
    for i in Json:
        content = {}
        for delstr in del_list:
            if i['data']['content']:
                i['data']['content'] = i['data']['content'].replace(delstr, '')
            else:
                i['data']['content'] = ""
        content['内容'] = i['data']['content']
        content['时间'] = i['time']
        if content['内容']:
            content_list.append(content)
    print(content_list)
    return content_list


def save(content_list):
    header = ['内容', '时间']
    with open('jinshi.csv', 'w', encoding='utf-8', newline='') as file_obj:
        # 创建写入器
        dictWriter = csv.DictWriter(file_obj, header)
        # 2.写表头
        dictWriter.writeheader()
        # 3.写入数据(一次性写入多行)
        dictWriter.writerows(content_list)


if __name__ == '__main__':
    save(get_jin10());
